/*
 * @(#)DocumentRetrievalManager.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.core;

import java.sql.Connection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.bean.DmsPersonalStorageSetup;
import com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.user.bean.MtmUserRecordUserGroup;
import com.dcivision.user.bean.MtmUserRecordUserRole;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;

/**
 * <p>Class Name:       DmsPersonalStorageSetupManager.java    </p>
 * <p>Description:      The class mainly handle the setup of the personal storage.</p>
 * @author              Jenny Li
 * @company             DCIVision Limited
 * @creation date       01/08/2003
 * @version             $Revision: 1.4 $
 */

public class DmsPersonalStorageSetupManager {

  public static final String REVISION = "$Revision: 1.4 $";

  public static final String FILE_SEPARATOR = "\\";

  protected Log log = LogFactory.getLog(this.getClass().getName());
  private DmsDocumentNameConvertor documentNameConvertor = null;
  private SessionContainer sessionContainer;
  private Connection conn;

  /**
   *  Constructor - Creates a new instance of DmsPersonalStorageSetupManager
   */
  public DmsPersonalStorageSetupManager(SessionContainer sessionContainer, Connection conn) {
    this.sessionContainer = sessionContainer;
    this.conn = conn;
  }

  /**
   *  Release - release all variable in instance of DmsPersonalStorageSetupManager
   */
  public synchronized void release() {
    this.sessionContainer = null;
    this.conn = null;
    documentNameConvertor = null;
  }
  
  /**
   * Checks whether a user has personal storage limit defined
   * @param userRecordID User record ID
   * @return true if the user has perosnal storage defined
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsPersonalStorageSetup
   */
  public boolean checkHasStorageUser(Integer userRecordID) throws ApplicationException{
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    DmsPersonalStorageSetup userStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userRecordID,DmsPersonalStorageSetup.MEMBER_TYPE_USER);
    if(!Utility.isEmpty(userStorageSetup)){
      return true;   
    }
    return false;
  }
  
  /**
   * Checks whether groups that user belongs to has personal storage limit defined
   * 
   * @param userRecordID user record ID
   * @return true if the group(s) that the user belongs to has already defined.
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsPersonalStorageSetup
   */
  public boolean checkHasStorageGroup(Integer userRecordID) throws ApplicationException{
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    List groupList=mtmUserRecordUserGroupDAO.getListByUserRecordID(userRecordID);
    for(int j=0;j<groupList.size();j++){
      MtmUserRecordUserGroup mtmUserRecordUserGroup=(MtmUserRecordUserGroup)groupList.get(j);
      DmsPersonalStorageSetup groupStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(mtmUserRecordUserGroup.getUserGroupID(),DmsPersonalStorageSetup.MEMBER_TYPE_GROUP);
      if(!Utility.isEmpty(groupStorageSetup)){
        return true;   
      }
    }  
    return false;
  }
  
  /**
   * Checks whether role(s) that user belongs to has personal storage limit defined
   * @param userRecordID user record ID
   * @return true if the role(s) that the user belongs to has already defined.
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsPersonalStorageSetup
   */
  public boolean checkHasStorageRole(Integer userRecordID) throws ApplicationException{
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    List roleList=mtmUserRecordUserRoleDAO.getListByUserRecordID(userRecordID);
    for(int j=0;j<roleList.size();j++){
      MtmUserRecordUserRole mtmUserRecordUserRole=(MtmUserRecordUserRole)roleList.get(j);
      DmsPersonalStorageSetup roleStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(mtmUserRecordUserRole.getUserRoleID(),DmsPersonalStorageSetup.MEMBER_TYPE_ROLE);
      if(!Utility.isEmpty(roleStorageSetup)){
        return true;   
      }
    }  
    return false;
  }
  
}
